﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using vJine.Core.IoC;

namespace vJineCore_QuickStart {
    public class QuickStart {

        public static void Run() {

            show_db_factories();

            bool isRun_quickstart_io = true;
            bool isRun_quickstart_appconfig = true;
            bool isRun_quickstart_orm = true;

            QuickStart.Run<QuickStart_IO>(isRun_quickstart_io);

            QuickStart.Run<QuickStart_AppConfig>(isRun_quickstart_appconfig);

            QuickStart.Run<QuickStart_ORM>(isRun_quickstart_orm);
        }

        static void show_db_factories() {
            DataTable dtFactories = DbProviderFactories.GetFactoryClasses();
            System.Console.WriteLine("Db Factories:{0}", dtFactories.Rows.Count);
            for(int i = 0, len = dtFactories.Rows.Count; i < len; i++) {
                DataRow drFactory = dtFactories.Rows[i];

                System.Console.WriteLine("Factory:{0}", i);
                System.Console.WriteLine("Name:{0}", drFactory["Name"]);
                System.Console.WriteLine("Description:{0}", drFactory["Description"]);
                System.Console.WriteLine("InvariantName:{0}", drFactory["InvariantName"]);
                System.Console.WriteLine("AssemblyQualifiedName:{0}", drFactory["AssemblyQualifiedName"]);
                try {
                    Type tFactory = Reflect.GetType(drFactory["AssemblyQualifiedName"] as string);
                    System.Console.WriteLine("Path:{0}", tFactory.Assembly.Location);
                } catch(Exception ex) {
                    System.Console.WriteLine("Path:<FAIL TO PARSE FACTORY>");
                }

            }
        }
        /// <summary>
        /// 开始运行测试实例
        /// </summary>
        public virtual void Start() {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 必须结束：如果为True则调用Stop方法
        /// </summary>
        public bool MustStop { get; set; }
        /// <summary>
        /// 结束运行测试实例
        /// </summary>
        public virtual void Stop() {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 执行测试循环
        /// </summary>
        /// <typeparam name="T">测试实体类型</typeparam>
        /// <param name="IsRun">是否执行循环：默认为True</param>
        public static void Run<T>(bool IsRun = true) where T : QuickStart, new() {
            if(!IsRun) {
                return;
            }

            T tInstance = new T();
            tInstance.Start();
            if(tInstance.MustStop) {
                tInstance.Stop();
            }
        }
    }
}
